
import { lazyReport } from '../report/report';
import {getDomPath} from "../utils/util";

// 手动检测上报,手动埋点时候用到
export function manualTrackerReport(actionType, data) {
  lazyReport('action', {
    actionType,
    data
  });
}

// 自动检测上报
export function autoTrackerReport() {
  document.body.addEventListener('click', function (e) {
    const clickedDom = e.target;

    // 获取标签上的data-action属性的值,处理属性埋点,含有data-action属性的元素,对其执行后返回传的信息是data-action属性的值
    let target = clickedDom?.getAttribute('data-action');

    // 获取标签上的data-no属性的值
    let no = clickedDom?.getAttribute('data-no');
    // 避免重复上报
    if (no) {
      return;
    }
    if (target) {
      lazyReport('action', {
        actionType: 'click',
        data: target
      });
    } else {
      // 获取被点击元素的dom路径
      const path = getDomPath(clickedDom);
      lazyReport('action', {
        actionType: 'click',
        data: path
      });
    }
  }, false);
}
